﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MachineQA.Model;
using MachineQA.DataAccess;

namespace MachineQA.Tests.Mocks
{
    public class MockMachineRepository : IMachineRepository
    {
        public event EventHandler<MachineAddedOrRemovedEventArgs> MachineAddedOrRemoved;

        readonly List<Machine> _machines;
        readonly List<Energy> _energies;

        public MockMachineRepository(List<Machine> machines, List<Energy> energies)
        {
            _machines = machines;
            _energies = new List<Energy>();
        }

        #region Public Interface

        public void AddMachine(Machine machine)
        {
            if (!_machines.Contains(machine))
            {
                _machines.Add(machine);
                if (this.MachineAddedOrRemoved != null)
                    this.MachineAddedOrRemoved(this, new MachineAddedOrRemovedEventArgs(machine, true));
            }
        }

        public void RemoveMachine(Machine machine)
        {
            if (machine == null)
                throw new ArgumentNullException("machine");

            if (_machines.Contains(machine))
            {
                _machines.Remove(machine);
                if (this.MachineAddedOrRemoved != null)
                    this.MachineAddedOrRemoved(this, new MachineAddedOrRemovedEventArgs(machine, false));
            }
        }

        public List<Machine> GetMachines()
        {
            return _machines;
        }

        public event EventHandler<MachineEnergiesModifiedEventArgs> MachineEnergiesModified;

        public void AddMachineEnergy(Machine machine, Energy energy)
        {
            if(!_machines.Contains(machine))
                throw new ArgumentException(Properties.Resources.AddingEnergyToNonExistentMachineArgumentException);
            if(!_energies.Contains(energy))
                throw new ArgumentException(Properties.Resources.AddingNonExistentEnergyArgumentException);

            if(!_machines.First(i => i == machine).Energies.Contains(energy))
            {
                _machines.First(i => i == machine).Energies.Add(energy);

                if(this.MachineEnergiesModified != null)
                    this.MachineEnergiesModified(this, new MachineEnergiesModifiedEventArgs(machine,
                        new List<Energy> {energy}, new List<Energy>()));
            }
        }

        public void RemoveMachineEnergy(Machine machine, Energy energy)
        {
            if (!_machines.Contains(machine))
                throw new ArgumentException(Properties.Resources.RemovingEnergyFromNonExistentMachineArgumentException);
            if (!_energies.Contains(energy))
                throw new ArgumentException(Properties.Resources.RemovingNonExistentEnergyFromMachineArgumentException);

            if (_machines.First(i => i == machine).Energies.Contains(energy))
            {
                _machines.First(i => i == machine).Energies.Remove(energy);

                if (this.MachineEnergiesModified != null)
                    this.MachineEnergiesModified(this, new MachineEnergiesModifiedEventArgs(machine,
                        new List<Energy>(), new List<Energy> { energy }));
            }
        }

        public bool ContainsMachine(Machine machine)
        {
            return _machines.Contains(machine);
        }

        #endregion // Public Interface
    }
}
